home *** CD-ROM | disk | FTP | other *** search
/ Freelog 22 / freelog 22.iso / Prog / Djgpp / GPC2952B.ZIP / doc / gpc / demos / arrayslicedemo.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-02-08  |  2.4 KB  |  83 lines

  1. {
  2. GPC demo program about array slice access (Extended Pascal).
  3.  
  4. Copyright (C) 1999-2001 Free Software Foundation, Inc.
  5.  
  6. Author: Frank Heckenbach <frank@pascal.gnu.de>
  7.  
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, version 2.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  20. Boston, MA 02111-1307, USA.
  21.  
  22. As a special exception, if you incorporate even large parts of the
  23. code of this demo program into another program with substantially
  24. different functionality, this does not cause the other program to
  25. be covered by the GNU General Public License. This exception does
  26. not however invalidate any other reasons why it might be covered
  27. by the GNU General Public License.
  28. }
  29.  
  30. program ArraySliceDemo;
  31.  
  32. procedure WriteArray (const a : array [m .. n : Integer] of Integer);
  33. var i : Integer;
  34. begin
  35.   Write ('(');
  36.   for i := m to n do
  37.     begin
  38.       Write ('''', a [i], '''');
  39.       if i < n then Write (', ')
  40.     end;
  41.   Writeln (')')
  42. end;
  43.  
  44. procedure ModifyArray (var a : array [m .. n : Integer] of Integer);
  45. var i : Integer;
  46. begin
  47.   for i := m to n do a [i] := Random (100)
  48. end;
  49.  
  50. var
  51.   s : String (60);
  52.   a : array [1 .. 10] of Integer;
  53.   i : Integer;
  54.  
  55. begin
  56.   { String slice access is similar to the Copy and SubStr functions ... }
  57.   s := 'With array slice access, you can access parts of an array.';
  58.   Writeln ('s = ''', s, '''');
  59.   Writeln ('s [6 .. 23] = ''', s [6 .. 23], '''');
  60.   Writeln;
  61.  
  62.   { ... but it can also be used to modify strings. }
  63.   Writeln ('Setting s [34 .. 39] := ''change''.');
  64.   s [34 .. 39] := 'change';
  65.   Writeln ('Now s = ''', s, '''');
  66.   Writeln;
  67.  
  68.   { Array slice access also works for non-string arrays ... }
  69.   Randomize;
  70.   for i := 1 to 10 do a [i] := Random (100);
  71.   Write ('a = ');
  72.   WriteArray (a);
  73.   Write ('a [3 .. 7] = ');
  74.   WriteArray (a [3 .. 7]);
  75.   Writeln;
  76.  
  77.   { ... and, of course, also for modifying. }
  78.   Writeln ('Modifying a [2 .. 6].');
  79.   ModifyArray (a [2 .. 6]);
  80.   Write ('Now a = ');
  81.   WriteArray (a)
  82. end.
  83.